package com.task.www.service;

import com.task.www.constant.ShowMerchant;
import com.task.www.constant.ShowRegister;
import com.task.www.dao.MechantData;
import com.task.www.po.Food;
import com.task.www.servlet.choose;
import com.task.www.servlet.RegisterNext;
import com.task.www.util.Clear;
import com.task.www.util.format;

import java.util.ArrayList;
import java.util.Scanner;

public class MainMerchant {
    public static void main(String[] args) {
        ShowRegister.showInterface(1);
        int number = choose.getnumber(1,2);
        Clear.clear();
        MechantData data = new MechantData();
        ShowMerchant sm = new ShowMerchant();
        String accout = RegisterNext.getaccout();
        String password = RegisterNext.getpassword();
        Scanner sn = new Scanner(System.in);
        String str = null;

        //注册账号
        if(number==1){
            System.out.println("\n");
            data.insertaccout(accout,password);
        }

        //登录账号
        if(number==2){
            while(!data.selectaccout(accout,password)){
                System.out.println("\n账号密码错误，请重新输入");
                accout = RegisterNext.getaccout();
                password = RegisterNext.getpassword();
            }
        }

        //商家主页面
        while(true) {
            int ids = data.getMechId(accout);
            Clear.clear();
            sm.showMerchant(accout);
            number = choose.getnumber(1,7);
            Clear.clear();
            if(number == 1){
                //修改店铺名称
                while(true){
                    sm.showname(accout);
                    number = choose.getnumber(1,2);
                    if(number == 1){
                       str = sn.next();
                       data.updatename(accout,str);
                    }
                    if(number == 2){
                        break;
                    }
                }
                continue;
            }

            if(number == 2){
                //修改店铺介绍
                while(true){
                    sm.showIntroduce(accout);
                    number = choose.getnumber(1,2);
                    if(number == 1){
                        str = sn.next();
                        data.updateIntroduce(accout,str);
                    }
                    if(number == 2){
                        break;
                    }
                }
                continue;
            }

            if(number == 3){
                //菜单
                while(true) {
                    int i = sm.showMenu(accout);
                    number = choose.getnumber(1, i);
                    if(number==i){
                        break;
                    }
                    else{
                        while(true) {
                            ArrayList<Food> foods = data.selectMenu(accout);
                            Food food = new Food(foods.get(number - 1).getName(), foods.get(number - 1).getPrice(), foods.get(number - 1).getIntroduce());
                            ArrayList<String> foods1 = data.selectFood(accout, food.getName());
                            sm.showFood(foods1);
                            int number2 = choose.getnumber(1,5);
                            Clear.clear();
                            if (number2 == 5) {
//                                返回
                                break;
                            }
                            System.out.print("请输入你所要修改菜品的");
                            if (number2 == 1) {
                                //菜名
                                System.out.println("名字：");
                            }
                            if (number2 == 2) {
//                                价格
                                System.out.println("价格：");
                            }
                            if (number2 == 3) {
//                                介绍
                                System.out.println("介绍：");
                            }
                            if (number2 == 4) {
//                                评论
                                System.out.println("评论：");
                            }
                            str = sn.next();
                            //判断输入的价格符合规则
                            if(number2==2){
                                while(!format.isNumber(str)){
                                    str = sn.next();
                                }
                            }
                            data.updateFood(ids,food.getName(),str,number2);
                        }
                    }
                }
                continue;
            }

            if(number == 4){
                //优惠券
                while(true) {
                    sm.operationCoupon();
                    int number1 = choose.getnumber(1, 4);
                    Clear.clear();
                    if(number1==1){
                        System.out.println("请输入优惠券使用门槛:");
                        int number2 = sn.nextInt();
                        System.out.println("请输入优惠券优惠金额:");
                        int number3 = sn.nextInt();
                        data.dmlcoupon(number2,number3,ids,number1);
                        System.out.println("添加成功,三秒后返回上一页面");
                        try {
                            Thread.sleep(3000);
                            continue;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if(number1==2){
                        System.out.println("请输入要删除优惠券使用门槛:");
                        int number2 = sn.nextInt();
                        System.out.println("请输入要删除优惠券优惠金额:");
                        int number3 = sn.nextInt();
                        data.dmlcoupon(number2,number3,ids,number1);
                        System.out.println("删除成功,三秒后返回上一页面");
                        try {
                            Thread.sleep(3000);
                            continue;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    if(number1==3){
                        System.out.println("请输入要修改优惠券使用门槛:");
                        int number2 = sn.nextInt();
                        System.out.println("请输入要修改优惠券优惠金额:");
                        int number3 = sn.nextInt();
                        System.out.println("请输入修改后的使用门槛:");
                        int number4 = sn.nextInt();
                        System.out.println("请输入修改后的优惠金额:");
                        int number5 = sn.nextInt();
                        data.dmlcoupon(number2,number3,ids,number1,number4,number5);
                        System.out.println("修改成功,三秒后返回上一页面");
                        try {
                            Thread.sleep(3000);
                            continue;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if(number1==4){
                        break;
                    }
                }
                continue;
            }

            if(number == 5){
                //接单
                ArrayList<String> buy = data.getbuy(ids);
                sm.showbuy(buy);
                int number1 = choose.getnumber(1,buy.size()+1);
                if (number1 == buy.size() + 1) {
                    continue;
                }else{
                    data.deletebuy(ids,buy.get(number1-1));
                    System.out.println("接单成功，三秒后返回主页面");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            if(number == 6){
                //刷新
                continue;
            }

            if(number == 7){
                //退出
                break;
            }
        }

    }
}
